package com.qulai.web.controller.amazon_us;

import com.qulai.amazon_us.domain.amazon_us.AmazonUsTag;
import com.qulai.amazon_us.service.amazon_us.IAmazonUsTagService;
import com.qulai.common.annotation.Log;
import com.qulai.common.core.controller.BaseController;
import com.qulai.common.core.domain.AjaxResult;
import com.qulai.common.core.page.TableDataInfo;
import com.qulai.common.enums.BusinessType;
import com.qulai.common.utils.http.HttpUtils;
import com.qulai.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.qulai.common.utils.http.HttpUtils.amazon_us_append_tag;

/**
 * 商品标签Controller
 * 
 * @author hunan
 * @date 2025-01-05
 */
@RestController
@RequestMapping("/amazon_us/tag")
public class AmazonUsTagController extends BaseController
{
    @Autowired
    private IAmazonUsTagService amazonUsTagService;

    /**
     * 查询商品标签列表
     */
    @PreAuthorize("@ss.hasPermi('amazon_us:tag:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmazonUsTag amazonUsTag)
    {
        startPage();
        List<AmazonUsTag> list = amazonUsTagService.selectAmazonUsTagList(amazonUsTag);
        return getDataTable(list);
    }

    /**
     * 导出商品标签列表
     */
    @PreAuthorize("@ss.hasPermi('amazon_us:tag:export')")
    @Log(title = "商品标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmazonUsTag amazonUsTag)
    {
        List<AmazonUsTag> list = amazonUsTagService.selectAmazonUsTagList(amazonUsTag);
        ExcelUtil<AmazonUsTag> util = new ExcelUtil<AmazonUsTag>(AmazonUsTag.class);
        util.exportExcel(response, list, "商品标签数据");
    }

    /**
     * 获取商品标签详细信息
     */
    @PreAuthorize("@ss.hasPermi('amazon_us:tag:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(amazonUsTagService.selectAmazonUsTagById(id));
    }

    /**
     * 新增商品标签
     */
    @PreAuthorize("@ss.hasPermi('amazon_us:tag:add')")
    @Log(title = "商品标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AmazonUsTag amazonUsTag)
    {
        return toAjax(0);
    }

    /**
     * 修改商品标签
     */
    @PreAuthorize("@ss.hasPermi('amazon_us:tag:edit')")
    @Log(title = "商品标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmazonUsTag amazonUsTag)
    {
        amazonUsTag.setUpdateBy(getUsername());
        if (amazonUsTag.getRemark() != null && amazonUsTag.getRemark().startsWith("tag")) {
            String fields = amazonUsTag.getRemark().replace("tag-","").replace("tag","");
            if (fields.contains("material")){
                fields += ",material_type";
            }
            try {
                Map<String, Object> params = new HashMap<>();
                params.put("basic_id", amazonUsTag.getBasicId());
                params.put("remark", fields);
                HttpUtils.sendPostToPython(amazon_us_append_tag, params);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
        return toAjax(amazonUsTagService.updateAmazonUsTag(amazonUsTag));
    }

    /**
     * 删除商品标签
     */
    @PreAuthorize("@ss.hasPermi('amazon_us:tag:remove')")
    @Log(title = "商品标签", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(amazonUsTagService.deleteAmazonUsTagByIds(ids));
    }
}
